package com.qiqijuice.sales;

import net.miginfocom.swt.MigLayout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.db4o.ObjectContainer;

public class CategoryCRUDView extends Composite {
	public CategoryCRUDView(Composite parent, int style) {
		super(parent, style);
		createView();
	}

	Text  txtName;
	Text  txtDesc;
	Button btnSave;
	Button btnDelete;
	Button btnNext;
	Button btnPrev;
	
	ProductCategory curCategory;
	
	void createView() {
		MigLayout layout = new MigLayout("fillx,filly", "[][][]40[right]", "[][]30[]");
		setLayout(layout);
		
		Group g = new Group(this, SWT.SHADOW_IN);
		g.setLayoutData("span 3 2, grow");
		g.setLayout(new MigLayout("fillx, filly, wrap 2", "[right]20[grow,fill]"));
		g.setText("Category Properties:");
		
		Label l = new Label(g, SWT.NONE );
		l.setText("Category Name:");
		
		Text t = new Text(g, SWT.SINGLE | SWT.BORDER);
		txtName = t;
		t.setLayoutData("");
		
		l = new Label(g, 0);
		l.setText("Description:");
		//l.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
		
		t = new Text(g, SWT.MULTI | SWT.BORDER);
		//t.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
		txtDesc = t;
		//t.setLayoutData("");

		Button b = new Button(this, SWT.PUSH);
		b.setText(">>");
		b.setLayoutData("wrap, wmin button, sg 1");
		btnNext = b;
		
		b = new Button(this, SWT.PUSH);
		b.setText("<<");
		b.setLayoutData("wrap, sg 1");
		btnPrev = b;
		
		b = new Button(this, SWT.PUSH);
		b.setText("Save");
		b.setLayoutData("cell 0 2, width 90::, sg 3");
		b.addSelectionListener(new SelectionAdapter() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				String name = txtName.getText();
				String desc = txtDesc.getText();
				
				ProductCategory cat = new ProductCategory(name, desc);
				
				if (validateConstraint(cat)) {
					curCategory.setName(name);
					curCategory.setDesc(desc);
					save(curCategory);
					AppFrame.getInst().getProductView().productCategoryChanged();
				}
			}

		});
		btnSave = b;
		
		b = new Button(this, SWT.PUSH);
		b.setText("Delete");
		b.setLayoutData("cell 1 2, sg 3");
		b.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				if (curCategory != null) {
					ObjectContainer oc = Db4oContainerFactory.getDB();
					oc.delete(curCategory);
					AppFrame.getInst().getProductView().productCategoryChanged();
				}
			}
			
		});
		btnDelete = b;
		
		viewCategory(null);
		
		pack();
		
	}

	public void viewCategory(ProductCategory pc) {
		boolean enabled = true;
		if (pc != null) {
			txtName.setText(pc.getName());
			txtDesc.setText(pc.getDesc());
		}
		else
		{
			txtName.setText("");
			txtDesc.setText("");
			enabled = false;
		}

		setEnabled(enabled);
		for (Control c : getChildren()) {
			c.setEnabled(enabled);
		}
		txtName.setEnabled(enabled);
		txtDesc.setEnabled(enabled);

		curCategory = pc;
	}
	
	boolean validateConstraint(ProductCategory p) {
		return true;
	}
	
	void save(ProductCategory p) {
		ObjectContainer oc = Db4oContainerFactory.getDB();
		oc.store(p);
		oc.commit();
		System.out.println("ProductCategory ["+p+"] saved!");
	}
	
	void resetTextFields() {
		txtName.setText("");
		txtDesc.setText("");
	}
}
